// basic/components/gcs-component/news/news-list.js
const app = getApp();
Component({
    options: {
        multipleSlots: true,
        addGlobalClass: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        listData: {
            type: Array,
            default: []
        },
        styleCode: { //列表样式
            type: String,
            default: "default"
        },
        pagepoint: {
            type: String,
            default: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        listData2: [],
        type: 'gcs-news-list',
        bThumbsUp: true, //能否点赞
        aThumbsUpApi: [], //0-点赞 1-取消点赞
        pageOperation: ["commit", "thumbs-up", "view", "share"]
    },
    observers: {
        'listData': function (listData) {
            this.formatData(listData);
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 使用 triggerEvent 方法触发自定义组件事件，指定事件名、detail对象和事件选项
        chooseClick: function (e) {
            var id = e.currentTarget.dataset.id;
            var type = this.data.type;
            this.triggerEvent('gcsEvent', {
                id,
                type
            }, {})
        },
        thumbsUp: function (e) {
            var _this = this;
            if (_this.data.bThumbsUp) {
                var index = e.currentTarget.dataset.index;
                var listData = _this.data.listData2;
                var apiname = _this.data.aThumbsUpApi[1]; //取消点赞
                if (!listData[index]['iZan']) {
                    apiname = _this.data.aThumbsUpApi[0]; //点赞的api
                }
                var tpObj = {};
                if (app.globalData.api_common_news[_this.data.pagepoint]['sThumbsUpPost'] != "") {
                    tpObj[app.globalData.api_common_news[_this.data.pagepoint]['sThumbsUpPost']] = listData[index]['iID'];
                } else {
                    tpObj['iID'] = listData[index]['iID'];
                }

                wx.$requestApi(apiname, tpObj, function (res) {
                    if (res.code == 0) { //更新页面数据
                        if (listData[index]['iZan'] == 1) { //已点赞 取消点赞
                            if (listData[index]['iNumzan'] > 0) {
                                listData[index]['iNumzan']--;
                            }
                        } else { //点赞
                            listData[index]['iNumzan']++;
                        }
                        listData[index]['iZan'] = !listData[index]['iZan']; //改变点赞状态
                        _this.setData({
                            listData2: listData
                        })
                    } else {
                        wx.$gcs_showToast(res.msg);
                    }
                })
            } else {
                wx.$gcs_showToast("功能未开放");
            }
        },
        commentPage: function (e) {
            wx.navigateTo({
                url: 'comment?id=' + this.data.listData[e.currentTarget.dataset.index]['iID'] + "&pagepoint=" + this.data.pagepoint,
            })
        },
        formatData: function (listData) {
            //数据显示处理
            for (var i = 0; i < listData.length; i++) {
                listData[i]['sTime'] = wx.$to_encodeTime(listData[i]['iTime'], "Y年M月D日");
                listData[i]['sUrl'] = `${app.globalData.link_upload_r}${listData[i]['sSources']}`;
            }
            this.setData({
                listData2: listData
            })
        }
    },
    //在组件实例进入页面节点树时执行
    // attached() {
    //     this.formatData();
    // },
    // //在组件在视图层布局完成后执行
    ready() {
        // this.formatData();
        var _this = this;
        var pagepoint = this.data.pagepoint;
        var api_common_news = app.globalData.api_common_news;
        if (api_common_news.hasOwnProperty(pagepoint)) {
            var opObj = {};
            for (var i = 0; i < api_common_news[pagepoint]['sOperation'].length; i++) {
                opObj[api_common_news[pagepoint]['sOperation'][i]] = true;
            }
            _this.setData({
                opObj: opObj
            })
            //检测是否存在点赞接口
            if (api_common_news[pagepoint]['sThumbsUpApi'].length >= 2 && api_common_news[pagepoint]['sThumbsUpApi'] != undefined) {
                _this.setData({
                    aThumbsUpApi: api_common_news[pagepoint]['sThumbsUpApi'],
                    bThumbsUp: true
                })
            }
        }
    },
    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: () => {},
        hide: () => {}
    },
})